#include "testdialog.h"

CustomDialog::CustomDialog(QWidget *parent):QDialog(parent)
{

}
TestDialog::TestDialog(QWidget *parent) : QWidget(parent)
{
    resize(800,600);
    init();
    //modalButton = new QPushButton("模态对话框",this);
    //connect(modalButton,&QPushButton::clicked,this,&TestDialog::displayDialog);

}

void TestDialog::displayDialog()
{
    dialog = new QDialog;
    //模态对话框，不能操作其他窗口
    //dialog->exec();
    //非模态对话框，可以操作其他窗口
    dialog->show();
    //当窗口关闭时，回收资源
    dialog->setAttribute(Qt::WA_DeleteOnClose);

}

void TestDialog::init()
{
    colorButton = new QPushButton("颜色对话框");
    errorButton = new QPushButton("错误信息对话框");
    fontButton = new QPushButton("字体对话框");
    inputButton = new QPushButton("输入对话框");
    fileButton = new QPushButton("文件对话框");
    messageButton = new QPushButton("警告对话框");
    proButton = new QPushButton("进度条对话框");
    wizardButton = new QPushButton("向导对话框");
    pushButton = new QPushButton("隐藏可扩展窗口");
    mainLayout = new QHBoxLayout;
    vLayout = new QVBoxLayout(this);
    tEdit = new QTextEdit;

    mainLayout->addWidget(colorButton);
    mainLayout->addWidget(errorButton);
    mainLayout->addWidget(fontButton);
    mainLayout->addWidget(inputButton);
    mainLayout->addWidget(fileButton);
    mainLayout->addWidget(messageButton);
    mainLayout->addWidget(proButton);
    mainLayout->addWidget(wizardButton);
    mainLayout->addWidget(pushButton);
    vLayout->addWidget(tEdit);
    vLayout->addLayout(mainLayout);

    connect(colorButton,&QPushButton::clicked,[=](){
        QColor co = QColorDialog::getColor(QColor("#ff8000"),this,"颜色");
        QPalette pa = colorButton->palette();
        pa.setBrush(QPalette::Button,QBrush(co));

        colorButton->setPalette(pa);
    });
    connect(errorButton,&QPushButton::clicked,[=](){
        QErrorMessage *error = QErrorMessage::qtHandler();
        error->showMessage("error!");
        error->show();
    });

    //使用获取到的字体修改文本框的字体
    connect(fontButton,&QPushButton::clicked,[=](){
        bool isOk;
        QFont fo = QFontDialog::getFont(&isOk,QFont("楷体"),this);
        tEdit->setFont(fo);
    });

    connect(inputButton,&QPushButton::clicked,[=](){
        int num = QInputDialog::getInt(this,"input","hello");
        tEdit->setText(QString::number(num));
    });

    connect(fileButton,&QPushButton::clicked,[=](){
       //QString filename = QFileDialog::getOpenFileName(this,"文件","./","all(*.*);;src(*.cpp *.c)");
       QStringList filename = QFileDialog::getOpenFileNames(this,"文件","./","all(*.*);;src(*.cpp *.c)");
       for(const auto &name : filename)
       {
           tEdit->append(name);
       }
    });

    connect(messageButton,&QPushButton::clicked,[=](){
        int ret = QMessageBox::warning(this,"警告","warning!",QMessageBox::Ok,QMessageBox::Cancel);
        if(QMessageBox::Ok == ret)
        tEdit->append("ok");
        else if(QMessageBox::Cancel == ret)
        tEdit->append("cancel");
    });

    connect(proButton,&QPushButton::clicked,[=](){
        QProgressDialog *pro = new QProgressDialog("progress...","cancel",0,100,this);
        pro->setValue(33);
        pro->show();
    });

    connect(wizardButton,&QPushButton::clicked,[=](){
        QWizard *wiz = new QWizard(this);
        wiz->addPage(new FirstPage);
        wiz->addPage(new SecondPage);
        wiz->addPage(new ThirdPage);
        wiz->show();
    });

     connect(pushButton,SIGNAL(clicked(bool)),SLOT(pushButton_clicked()));
}

void TestDialog::pushButton_clicked()
{
    tEdit->setVisible(!tEdit->isVisible());
    if(tEdit->isVisible())
    {
        pushButton->setText(tr("隐藏可扩展窗口"));
    }
    else
    {
        pushButton->setText(tr("显示可扩展窗口"));
    }
}


